﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apps.IBLL;
using Apps.Common;
using Microsoft.Practices.Unity;
using Apps.IDAL;
using Apps.BLL.Core;
using Apps.Models;
using Apps.IDAL.Sys;
using Apps.IBLL.Sys;
using Apps.Models.Sys;

namespace Apps.BLL.Sys
{
    public class SysRightGetModuleRightBLL : ISysRightGetModuleRightBLL
    {
        [Dependency]
        public ISysModuleRepository moduleRepository { get; set; }
        [Dependency]
        public ISysRightGetModuleRightRepository sysRightGetModuleRightRepository { get; set; }
        public object GetPerantModule(GridPager pager, string nodeid, string parentid, int? n_level)
        {
            string pid = string.IsNullOrEmpty(nodeid) ? "0" : nodeid;

            int level = (n_level == null ? 0 : Convert.ToInt32(n_level) + 1);
            var parentModule = from r in moduleRepository.GetList()
                               where r.IsLast == false
                               orderby r.Id
                               select r;

            int totalRecords = parentModule.Count();

            var modules = parentModule.Where(a => a.ParentId == pid && a.Id != "0").OrderBy(a => a.Sort).ToList();
            var moduleChilds = from r in moduleRepository.GetList()
                               where r.IsLast == false
                               orderby r.Id
                               select r;


            var jsonData = new
            {
                total = 1,
                page = 1,
                records = totalRecords,
                rows = (
                    from r in modules
                    select new
                    {
                        cell = new string[]
                            {
                                r.Id.ToString(),//模块ID
                                r.Name,         //
                                level.ToString(),            //Level:the current row level,0=root row,1=first row of root,and so on.
                                pid,//ParentId :The parent row id of the current cell,null=no parent.
                                //测试当前记录是否还有子项
                                moduleChilds.Where(a=>a.ParentId==r.Id).Count()>0?"false":"true",  
                                "false"         //Is expanded:if it is expanded when the first loading?
                            }
                    }

                ).ToArray()

            };

            return jsonData;
        }

        /// <summary>
        /// 取模块操作项
        /// </summary>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public List<SysModuleOperate> GetModuleOperateByModuleId(string moduleId)
        {
            return sysRightGetModuleRightRepository.GetList().Where(a => a.ModuleId == moduleId).ToList();
        }


        public GridRows<RoleRight> GetModuleRoleRight(string moduleId)
        {
            using (DBContainer db = new DBContainer())
            {
                var mModule = db.SysModule.Where(m => m.Id == moduleId).FirstOrDefault();
                if (mModule == null || mModule.SysRight == null)
                {
                    return new GridRows<RoleRight>();
                }
                var RoleRights = new List<RoleRight>();
                var SysModuleOperate = new Dictionary<string,List<SysModuleOperate>>();
                foreach (var Right in mModule.SysRight)
                {
                    if (RoleRights.Where(m => m.Ids == Right.RoleId).Count() == 0)
                    {
                        var mRoleRight = new RoleRight();
                        mRoleRight.Ids = Right.RoleId;
                        mRoleRight.RoleName = Right.SysRole.Name;
                        SysModuleOperate[mRoleRight.Ids] = mModule.SysModuleOperate.ToList();
                        RoleRights.Add(mRoleRight);
                    }
                    
                }

                foreach (var RoleRight in RoleRights)
                {
                    var SelRightOperate = new List<SysRightOperate>();
                    foreach (var SysRightOperates in mModule.SysRight.Where(m => m.RoleId == RoleRight.Ids).Select(m => m.SysRightOperate))
                    {
                        foreach (var SysRightOperate in SysRightOperates.Where(m => m.IsValid))
                        {
                            if (SelRightOperate.Where(m => m.KeyCode == SysRightOperate.KeyCode).Count() == 0)
                            {
                                SelRightOperate.Add(SysRightOperate);
                            }
                        }
                    }
                    foreach (var ModuleOperat in SysModuleOperate[RoleRight.Ids])
                    {
                        if (SelRightOperate.Where(m => m.KeyCode == ModuleOperat.KeyCode).Count() > 0)
                        {
                            RoleRight.RightList += "<a class=\"fa fa-check-square-o color-green\"/>" + ModuleOperat.Name;
                        }
                        else
                        {
                            RoleRight.RightList += "<a class=\"fa fa-square-o color-red\"/>" + ModuleOperat.Name;
                        }
                    }
                }

                return new GridRows<RoleRight>() { rows = RoleRights, total = RoleRights.Count() };
            }
        }

        public GridRows<UserRight> GetModuleUserRight(string moduleId)
        {
            using (DBContainer db = new DBContainer())
            {
                var mModule = db.SysModule.Where(m => m.Id == moduleId).FirstOrDefault();
                if (mModule == null || mModule.SysRight == null)
                {
                    //没有查询到任何匹配的数据
                    return new GridRows<UserRight>();
                }
                var UserRights = new List<UserRight>();
                var SysModuleOperate = new Dictionary<string, List<SysModuleOperate>>();

                foreach (var Right in mModule.SysRight)
                {
                    foreach (var User in Right.SysRole.SysUser)
                    {
                        if (UserRights.Where(m => m.Ids == User.Id).Count() == 0)
                        {
                            var mUserRight = new UserRight();
                            mUserRight.Ids = User.Id;
                            mUserRight.UserName = User.TrueName;
                            SysModuleOperate[mUserRight.Ids] = mModule.SysModuleOperate.ToList(); 
                            UserRights.Add(mUserRight);
                        }
                    }
                }

                foreach (var UserRight in UserRights)
                {
                    var SelRightOperate = new  List< SysRightOperate>();
                    foreach (var SysRightOperates in mModule.SysRight.Where(m=>m.SysRole.SysUser.Where(m1=>m1.Id==UserRight.Ids).Count()>0).Select(m=>m.SysRightOperate))
                    {
                        foreach (var SysRightOperate in SysRightOperates.Where(m=>m.IsValid))
                        {
                            if (SelRightOperate.Where(m => m.KeyCode == SysRightOperate.KeyCode).Count() == 0)
                            {
                                SelRightOperate.Add(SysRightOperate);
                            }
                        }
                    }
                    foreach (var ModuleOperat in SysModuleOperate[UserRight.Ids])
                    {
                        if (SelRightOperate.Where(m => m.KeyCode == ModuleOperat.KeyCode).Count() > 0)
                        {
                            UserRight.RightList += "<a class=\"fa fa-check-square-o color-green\"/>" + ModuleOperat.Name;
                        }
                        else
                        {
                            UserRight.RightList += "<a class=\"fa fa-square-o color-red\"/>" + ModuleOperat.Name;
                        }
                    }
                }

                return new GridRows<UserRight>() { rows = UserRights, total = UserRights.Count() };
            }
        }
    }
}
